{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 3\n",
    "b = 4\n",
    "c = 5.66\n",
    "d = 8.0\n",
    "e = complex(c, d)\n",
    "f = complex(float(a), float(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a is type <class 'int'>\n",
      "b is type <class 'int'>\n",
      "c is type <class 'float'>\n",
      "d is type <class 'float'>\n",
      "e is type <class 'complex'>\n",
      "f is type <class 'complex'>\n"
     ]
    }
   ],
   "source": [
    "print (\"a is type\" , type(a))\n",
    "print (\"b is type\" , type(b))\n",
    "print (\"c is type\" , type(c))\n",
    "print (\"d is type\" , type(d))\n",
    "print (\"e is type\" , type(e))\n",
    "print (\"f is type\" , type(f))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "1.4134275618374559\n",
      "1.3333333333333333\n",
      "1\n",
      "(5.66+8j)\n",
      "(8.66+12j)\n"
     ]
    }
   ],
   "source": [
    "print(a + b)\n",
    "print(d / c)\n",
    "print (b / a)\n",
    "print (b // a)\n",
    "print (e)\n",
    "print (e + f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "e's real part is:  5.66\n",
      "e's imaginary part is:  8.0\n",
      "sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)\n"
     ]
    }
   ],
   "source": [
    "print (\"e's real part is: \" , e.real)\n",
    "print (\"e's imaginary part is: \" , e.imag)\n",
    "print (sys.float_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tom was 3 years old\n",
      "Tom was 3 years old\n",
      "What's your name? \n",
      "Tom\n"
     ]
    }
   ],
   "source": [
    "age = 3\n",
    "name = \"Tom\"\n",
    "print(\"{0} was {1} years old\".format(name, age))\n",
    "print(name + \" was \" + str(age) + \" years old\")\n",
    "print(\"What's your name? \\nTom\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字面值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6, 2.24, 0.00345, 'This is a string')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "6, 2.24, 3.45e-3, \"This is a string\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "identifier = 8"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_list = [1, 3, 5, 7, 9]\n",
    "string_list = [\"abc\", \"bbc\", \"python\"]\n",
    "mixed_list = ['python', 'java', 3, 12]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "second_num = number_list[1]\n",
    "third_string = string_list[2]\n",
    "fourth_mix = mixed_list[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "second_num: 3 third_string: python fourth_mix: 12\n"
     ]
    }
   ],
   "source": [
    "print(\"second_num: {0} third_string: {1} fourth_mix: {2}\".format(second_num, third_string, fourth_mix))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number_list before: [1, 3, 5, 7, 9]\n",
      "number_list after: [1, 30, 5, 7, 9]\n"
     ]
    }
   ],
   "source": [
    "print(\"number_list before: \" + str(number_list))\n",
    "number_list[1] = 30\n",
    "print(\"number_list after: \" + str(number_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mixed_list before delete: ['python', 'java', 3, 12]\n",
      "mixed_list after delete: ['python', 'java', 12]\n"
     ]
    }
   ],
   "source": [
    "print(\"mixed_list before delete: \" + str(mixed_list))\n",
    "del mixed_list[2]\n",
    "print(\"mixed_list after delete: \" + str(mixed_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "[1, 2, 3, 4, 5, 6]\n",
      "['Hello', 'Hello', 'Hello', 'Hello']\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(len([1,2,3])) #长度\n",
    "print([1,2,3] + [4,5,6]) #组合\n",
    "print(['Hello'] * 4) #重复\n",
    "print(3 in [1,2,3]) #某元素是否在列表中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b\n",
      "c\n",
      "['b', 'c', 'd']\n",
      "['a', 'b', 'c']\n"
     ]
    }
   ],
   "source": [
    "abcd_list =['a', 'b', 'c', 'd'] \n",
    "print(abcd_list[1])\n",
    "print(abcd_list[-2])\n",
    "print(abcd_list[1:])\n",
    "print(abcd_list[:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "a_list = [1, 3, 5, 7, 9]\n",
    "\n",
    "for i in a_list:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "列表操作包含以下函数:\n",
    "\n",
    "1. `cmp(list1, list2)`：比较两个列表的元素 \n",
    "2. `len(list)`：列表元素个数 \n",
    "3. `max(list)`：返回列表元素最大值 \n",
    "4. `min(list)`：返回列表元素最小值 \n",
    "5. `list(seq)`：将元组转换为列表 \n",
    "\n",
    "列表操作包含以下方法:\n",
    "\n",
    "1. `list.append(obj)`：在列表末尾添加新的对象\n",
    "2. `list.count(obj)`：统计某个元素在列表中出现的次数\n",
    "3. `list.extend(seq)`：在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）\n",
    "4. `list.index(obj)`：从列表中找出某个值第一个匹配项的索引位置\n",
    "5. `list.insert(index, obj)`：将对象插入列表\n",
    "6. `list.pop(obj=list[-1])`：移除列表中的一个元素（默认最后一个元素），并且返回该元素的值\n",
    "7. `list.remove(obj)`：移除列表中某个值的第一个匹配项\n",
    "8. `list.reverse()`：反向列表中元素\n",
    "9. `list.sort([func])`：对原列表进行排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "a_tuple = (2,) #创建只有一个元素的tuple，需要用逗号结尾消除歧义\n",
    "mixed_tuple = (1, 2, ['a', 'b'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2,)\n",
      "mixed_tuple: (1, 2, ['a', 'b'])\n"
     ]
    }
   ],
   "source": [
    "print(a_tuple)\n",
    "print(\"mixed_tuple: \" + str(mixed_tuple))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mixed_tuple: (1, 2, ['c', 'd'])\n"
     ]
    }
   ],
   "source": [
    "mixed_tuple[2][0] = 'c'\n",
    "mixed_tuple[2][1] = 'd'\n",
    "print(\"mixed_tuple: \" + str(mixed_tuple))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "v = ('a', 'b', 'e')\n",
    "(x, y, z) = v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('a', 'b', 'e')\n",
      "a b e\n"
     ]
    }
   ],
   "source": [
    "print(v)\n",
    "print(x, y, z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "a_tuple = (1, 3, 5, 7, 9)\n",
    "\n",
    "for i in a_tuple:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tom's number is 123\n",
      "Tom is a boy\n"
     ]
    }
   ],
   "source": [
    "phone_book = {'Tom': 123, \"Jerry\": 456, 'Kim': 789}\n",
    "mixed_dict = {\"Tom\": 'boy', 11: 23.5}\n",
    "print(\"Tom's number is \" + str(phone_book['Tom']))\n",
    "print('Tom is a ' + mixed_dict['Tom'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "phone_book: {'Tom': 999, 'Jerry': 456, 'Kim': 789, 'Heath': 888}\n"
     ]
    }
   ],
   "source": [
    "phone_book['Tom'] = 999\n",
    "phone_book['Heath'] = 888\n",
    "print(\"phone_book: \" + str(phone_book))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "updated phone_book: {'Tom': 999, 'Jerry': 456, 'Kim': 789, 'Heath': 888, 'Ling': 159, 'Lili': 247}\n"
     ]
    }
   ],
   "source": [
    "phone_book.update({'Ling':159, 'Lili':247})\n",
    "print(\"updated phone_book: \" + str(phone_book))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "phone_book after deleting Tom: {'Jerry': 456, 'Kim': 789, 'Heath': 888, 'Ling': 159, 'Lili': 247}\n"
     ]
    }
   ],
   "source": [
    "del phone_book['Tom']\n",
    "print(\"phone_book after deleting Tom: \" + str(phone_book)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "after clear: {}\n"
     ]
    }
   ],
   "source": [
    "phone_book.clear()\n",
    "print(\"after clear: \" + str(phone_book))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "del phone_book\n",
    "# print(\"after del: \" + str(phone_book)) # Error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "list_dict = {('Name'): 'John', 'Age':13} # OK\n",
    "# list_dict = {['Name']: 'John', 'Age':13} # Error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tom\n",
      "111\n",
      "Jerry\n",
      "222\n",
      "Cathy\n",
      "333\n",
      "Tom 111\n",
      "Jerry 222\n",
      "Cathy 333\n"
     ]
    }
   ],
   "source": [
    "a_dict = {'Tom':'111', 'Jerry':'222', 'Cathy':'333'}\n",
    "\n",
    "for ele in a_dict:\n",
    "    print(ele)\n",
    "    print(a_dict[ele])\n",
    "    \n",
    "for key, elem in a_dict.items():\n",
    "    print(key, elem)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python字典包含了以下内置函数：\n",
    "\n",
    "1. `cmp(dict1, dict2)`：比较两个字典元素。\n",
    "2. `len(dict)`：计算字典元素个数，即键的总数。\n",
    "3. `str(dict)`：输出字典可打印的字符串表示。\n",
    "4. `type(variable)`：返回输入的变量类型，如果变量是字典就返回字典类型。\n",
    "\n",
    "Python字典包含了以下内置方法：\n",
    "\n",
    "1. `radiansdict.clear()`：删除字典内所有元素\n",
    "2. `radiansdict.copy()`：返回一个字典的浅复制\n",
    "3. `radiansdict.fromkeys()`：创建一个新字典，以序列 seq 中元素做字典的键，val 为字典所有键对应的初始值\n",
    "4. `radiansdict.get(key, default=None)`：返回指定键的值，如果值不在字典中返回 default 值\n",
    "5. `radiansdict.has_key(key)`：如果键在字典 dict 里返回 `true`，否则返回 `false`\n",
    "6. `radiansdict.items()`：以列表返回可遍历的(键, 值) 元组数组\n",
    "7. `radiansdict.keys()`：以列表返回一个字典所有的键\n",
    "8. `radiansdict.setdefault(key, default=None)`：和 `get()` 类似, 但如果键不已经存在于字典中，将会添加键并将值设为 default\n",
    "9. `radiansdict.update(dict2)`：把字典 dict2 的键/值对更新到 dict 里\n",
    "10. `radiansdict.values()`：以列表返回字典中的所有值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
